const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');

const dbConfig = {
  host : "139.129.42.180",
  port : 3306,
  name : "mysql",
  user : "bian",
  pwd : "Bln12345",
  dbname : "airport",
  toString() {
    return `${this.name}://${this.user}:${this.pwd}@${this.host}:${this.port}/${this.dbname}`;
  }
};

let sequelize = new Sequelize(dbConfig.toString(), {
  logging: () => {}
});

fs
  .readdirSync(__dirname)
  .filter((filename, index) => {
    return filename !== 'index.js' && filename !== 'migrate.js';
  }).map((filename) => {
    sequelize.import(path.join(__dirname, filename));
});

let models = sequelize.models;
Object.keys(models).forEach((tableName) => {
  if(models[tableName].options.hasOwnProperty("associate")){
    models[tableName].options.associate(models);
  }
});

module.exports = sequelize;
